﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

public class AllOrder : IOrderService    //所有订单
{
    public List<Order> order = new List<Order>();

    private AllOrder()
    {

    }
    private static AllOrder allOrder = new AllOrder();

    public static AllOrder getInstance()
    {

        return AllOrder.allOrder;
    }

    public void export()
    {
        XmlSerializer a = new XmlSerializer(typeof(List<Order>));
        using (FileStream b = new FileStream("order.xml", FileMode.Create))
        {
            a.Serialize(b, this.order);
        }
        Console.WriteLine("序列化完成");
    }

    public void import()
    {
        try
        {
            XmlSerializer a = new XmlSerializer(typeof(List<Order>));
            using (FileStream b = new FileStream("order.xml", FileMode.Open))
            {
                List<Order> c = (List<Order>)a.Deserialize(b);
                Console.WriteLine("反序列化结果：");
                foreach (Order d in c)
                {
                    Console.WriteLine("订单号 客户 日期 总金额");
                    Console.WriteLine("----------------------------");
                    Console.WriteLine("{0} {1} {2} {3}", d.Id, d.Customer, d.Date, d.Money);
                    d.showOrderItem();
                }
            }
        }
        catch
        {
            Console.WriteLine("序列化系列操作错误");
        }
    }
    public void ShowOrder()
    {

        foreach (Order a in this.order)
        {
            Console.WriteLine("订单号 客户 日期 总金额");
            Console.WriteLine("----------------------------");
            Console.WriteLine("{0} {1} {2} {3}", a.Id, a.Customer, a.Date, a.Money);
            a.showOrderItem();
        }
    }
    public void addOrder(int id, string customer, string date, List<OrderItem> orderItems)        //增加订单
    {
       
        
            Order a = new Order(id, customer, date);


            bool same = false;
            foreach (Order m in this.order)
            {
                if (m.Equals(a)) same = true;
            }
            if (same) Console.WriteLine("订单号重复");
            else
            {
                foreach (OrderItem item in orderItems) {
                    a.addOrderItem(item.Name, item.Number, item.Price);

                } }

            order.Add(a);
            a.getAllPrice();
            Console.WriteLine("建立成功");
           
        
        


    }
    public void removeOrder(int id)           //删除订单
    {
        try
        {
            
            
            int index = 0;
            foreach (Order a in this.order)
            {
                if (a.Id == id) index = this.order.IndexOf(a);
            }
            
            this.order.RemoveAt(index);  
                
            
        }
        catch
        {
            Console.WriteLine("输入错误");
        }

    }

    public List<Order> searchOrder(int i, string input)  //查询订单
    {

        switch (i)
        {
            case 1:

                var query1 = from s1 in order
                             where int.Parse(input) == s1.Money
                             orderby s1.Money
                             select s1;

                List<Order> a1 = query1.ToList();
                return a1;


            case 2:

                var query2 = from s2 in order
                             where s2.Customer == input

                             orderby s2.Money
                             select s2;

                List<Order> a2 = query2.ToList();
                return a2;


            case 3:
                             var query4 = from s3 in order
                             where s3.Id == int.Parse(input)
                             orderby s3.Money
                             select s3;

                List<Order> a3 = query4.ToList();
                return a3;


            default:
                return null;





        }
    }

}


